<template>
  <view class="container">
    <!--  带搜索的标题栏  -->
    <view class="back-layout hor-layout">
      <view class="back-view" @click="navBack">
        <i class="iconfont back-icon">&#xe8de;</i>
      </view>
      <u-search margin="8px" placeholder="请输入员工姓名" v-model="search" :action-style="searchStyle"
                @search="refreshData" @custom="refreshData" @clear="refreshData"></u-search>
    </view>
    <view class="student-scroll-view">
      <block v-if="teacherList.length>0">
        <view v-for="(item,index) in teacherList" :key="index" class="staff-item hor-layout-center"
              @click="toTeacherInfo(item)">
          <view v-if="from" class="check-box" @click.stop="checkTeacher(item)">
            <view class="check-view ver-layout-center-all" :class="{'select':item.id===selectTeaId}">
              <i class="iconfont check-icon">&#xe72e;</i>
            </view>
          </view>
          <image class="staff-icon" :src="item.avatar?item.avatar:ic_teacher_man_default"/>
          <view class="staff-info ver-layout">
            <view class="name">{{ item.name }}</view>
            <view class="phone">联系电话：{{ item.phone }}</view>
            <view class="job">所在岗位：{{ item.jobs&&item.jobs.map(j => j.job_name).join("，") }}</view>
          </view>
          <view class="staff-state" :class="{'disabled':getStaffState(item).disabled}">
            {{ getStaffState(item).text }}
          </view>
        </view>
        <u-loadmore margin-top="15" v-if="loadingObj.status" :status="loadingObj.status"/>
      </block>
      <view v-else class="list-no-data"> 暂无数据</view>
      <view style="height: 60px"></view>
    </view>
    <!-- 添加按钮   -->
    <view v-if="!from" class="add-student-btn" @click="toAddStaff">
      <i class="iconfont add-student-icon">&#xe601;</i>
    </view>
    <!-- 确认   -->
    <view v-if="from" class="confirm-layout hor-layout-center-all">
      <view class="confirm-view" :class="{'disabled':!canSaveUse}" @click="confirmSelect">确认</view>
    </view>
  </view>
</template>

<script>
import {addStaff, staffDetail} from "@/router.js"
import {StaffListApi} from "@/api/staffList.js"

let ic_teacher_man_default = require("@/static/home/ic_teacher_man_default.png")
let engine = new StaffListApi()
export default {
  name: "staffList",
  data() {
    return {
      ic_teacher_man_default,
      searchStyle: {
        color: 'white',
        width: '60px',
        height: '28px',
        lineHeight: '28px',
        borderRadius: '14px',
        background: '#ff9900',
      },
      from: '',
      search: '',
      teacherList: [{}],
      selectTeaId: '',
      loadingObj: {
        status: '',
        current: 1,
        total: ''
      },
    }
  },
  computed: {
    canSaveUse() {
      return this.selectTeaId
    }
  },
  onLoad(options) {
    let {from} = options
    this.from = from
    this.refreshData()
  },
  onReachBottom(e) {
    this.loadMore()
  },
  methods: {
    checkTeacher(item) {
      this.selectTeaId = item.id
    },
    getStaffState(item) {
      let stateMap = {0: '离职', 1: '在职'}
      return {disabled: item.status === 0, text: stateMap[item.status]}
    },
    getTeacherList() {
      let search = this.search, page = this.loadingObj.current
      engine.getStaffList({search, page}).then(res => {
        debugger
        this.teacherList = this.teacherList.concat(res.data)
        this.loadFinish(res.total)
      })
    },
    refreshData() {
      this.loadingObj.current = 1
      this.loadingObj.status = ''
      this.teacherList = []
      this.getTeacherList()
    },
    loadMore() {
      if (this.loadingObj.current >= this.loadingObj.total) {
        this.loadingObj.status = 'nomore'
      } else {
        this.loadingObj.status = 'loading'
        this.loadingObj.current = this.loadingObj.current + 1
        this.getTeacherList()
      }
    },
    loadFinish(total) {
      this.loadingObj.total = Math.ceil(total / 10)
      this.loadingObj.status = ''
      if (this.loadingObj.current >= this.loadingObj.total
          && this.teacherList.length > 0) {
        this.loadingObj.status = 'nomore'
      }
    },
    navBack() {
      this.$u.route({type: 'back'})
    },
    toTeacherInfo(item) {
      this.$u.route({url: staffDetail, params: {...item, from: this.from}})
    },
    toAddStaff() {
      this.$u.route({url: addStaff})
    },
    confirmSelect() {
      let selTea = this.teacherList.find(tea => tea.id === this.selectTeaId)
      let pages = getCurrentPages();
      console.log("confirmSelect", this.from, pages)
      let createClassPage = pages.find(page => page.route === this.from)
      createClassPage.$vm.confirmSelectAdviser(selTea);//重点$vm
      this.$u.route({type: 'back'})
    },
  }
}
</script>

<style>
page {
  background: #F2F2F2;
}

.back-layout {
  z-index: 1000;
  position: fixed;
  width: 100%;
  padding-top: var(--status-bar-height);
  height: calc(var(--status-bar-height) + 44px);
  line-height: 44px;
  font-size: 16px;
  background: #42B4AC;
}

.back-view {
  padding-left: 9px;
  padding-right: 9px;
}

.back-icon {
  color: white;
  font-size: 22px;
}

.container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--window-top));
  width: 100%;
}

.student-scroll-view {
  margin-top: calc(44px + var(--status-bar-height));
  width: 100%;
  flex: 1;
}

.staff-item {
  padding: 22px 12px;
  margin: 0px 12px;
  margin-top: 18px;
  background: #FFFFFF;
  border-radius: 5px;
}

.check-box {
  padding: 12px;
}

.check-view {
  width: 16px;
  height: 16px;
  line-height: 16px;
  text-align: center;
  border-radius: 50%;
  background: white;
  border: 1px solid gray;
}

.check-view.select {
  background: #42B4AB;
  border: 1px solid #42B4AB;
}

.check-icon {
  color: white;
  font-size: 10px;
}

.staff-icon {
  width: 38px;
  height: 38px;
  margin-right: 6px;
  border-radius: 3px;
}

.add-student-btn {
  position: fixed;
  bottom: 33px;
  right: 13px;
  width: 58px;
  height: 58px;
  line-height: 58px;
  text-align: center;
  background: #6079F5;
  border-radius: 50%;
}

.add-student-icon {
  color: white;
  font-size: 30px;
}

.staff-info {
  flex: 1;
  margin-left: 11px;
}

.staff-info > .name {
  font-size: 16px;
  line-height: 16px;
  color: #333333;
}

.staff-info > .phone {
  margin-top: 5px;
  font-size: 12px;
  line-height: 12px;
  color: #333333;
}

.staff-info > .job {
  margin-top: 5px;
  font-size: 12px;
  line-height: 12px;
  color: #333333;
}

.staff-state {
  width: 40px;
  height: 17px;
  text-align: center;
  line-height: 17px;
  background: #42B4AB;
  border-radius: 3px;
  /**/
  font-size: 12px;
  color: #FFFFFF;
}

.staff-state.disabled {
  color: #909399;
  background: #EAEAEA;
}

.confirm-layout {
  z-index: 1000;
  width: 100%;
  height: 50px;
  position: fixed;
  bottom: 0px;
  background: #F2F2F2;
  border-top: 1px solid white;
}

.confirm-view {
  margin: auto;
  width: 70%;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background: #42B4AB;
  border-radius: 17px;
  font-size: 16px;
  color: white;
}

.confirm-view.disabled {
  background: #c9c9c9;
}
</style>
